package it.fuesi.myseries.activities;

import it.fuesi.myseries.R;
import it.fuesi.myseries.api.ApiBrokerService;
import it.fuesi.myseries.api.ApiMethod;
import it.fuesi.myseries.model.MySeriesConstant;
import it.fuesi.myseries.model.Serie;
import it.fuesi.myseries.model.adapters.SerieItemAdapter;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView.LayoutParams;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SearchView;
import android.widget.Toast;

public class MainActivity extends Activity implements Observer {

	//private ArrayList<Serie> searchResults;
	//private ArrayAdapter<Serie> searchResultsAdapter;
	
	private ArrayList<Serie> mySeries;
	private SerieItemAdapter mySeriesAdapter;
	
	
	//private ListView listResults;
	private ListView mySeriesList;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mySeriesList = (ListView)findViewById(R.id.mySeriesList);
		mySeries = new ArrayList<Serie>();
		mySeriesAdapter = new SerieItemAdapter(this,
				R.layout.serie_list_item, mySeries);
		
		
		mySeriesList.setAdapter(mySeriesAdapter);
		
		mySeriesList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int index,
					long arg3) {
				openDetail(mySeries.get(index));
			}
		});
		
		
		getMySeries();
		
	}
	
	
	public void reloadMySeries(MenuItem item){
		getMySeries();
	}
	
	
	@Override
	protected void onResume() {
		getMySeries();
		super.onResume();
	}
	
	public void openDetail(Serie serie) {
		Intent intent = new Intent(this, MySerieDetailActivity.class);
		intent.putExtra("EXTRA_MESSAGE", serie);
		startActivity(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		
		final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
		final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
		searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
		
		return true;
	}
	
	public void getMySeries(){
		ProgressBar p = new ProgressBar(mySeriesList.getContext());
		p.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT, Gravity.CENTER));
		p.setIndeterminate(true);
		mySeriesList.setEmptyView(p);
		
		Intent intent = ApiBrokerService.getApiTaskIntent(this, ApiMethod.getMySeries);
		
		startService(intent);
	}


	@Override
	public void update(Observable observable, Object results) {
		//searchResultsAdapter.clear();
		mySeriesAdapter.clear();
		
		final Bundle bundle = (Bundle) results;
		
		if(bundle.getBoolean(MySeriesConstant.SUCCESS.toString())){
		
			final Bundle data = bundle.getBundle(MySeriesConstant.DATA.toString());
			
			for (String key : data.keySet()) {
				final Serie s = (Serie) data.getSerializable(key);
				//searchResultsAdapter.add(s);
				mySeriesAdapter.add(s);
			}
		}else{
			//String msg = "Search failed! " + bundle.getString(MySeriesConstant.ERROR.toString());
			//Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
		}
			

	}

}
